package com.yc.exam.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.exam.entity.User;
import com.yc.exam.service.IAnswerResultsService;
import com.yc.exam.service.IUserService;
import com.yc.exam.vo.AnswerTopicVo;
import com.yc.exam.vo.MarkingVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 答题结果 前端控制器
 * </p>
 *
 * @author exam
 * @since 2022-05-15
 */
@RestController
@RequestMapping("/exam/answerResults")
@AllArgsConstructor
public class AnswerResultsController {

    private final IAnswerResultsService answerResultsService;

    private final IUserService userService;

    @GetMapping("page")
    public Page<AnswerTopicVo> getPage(Page<AnswerTopicVo> answerTopicVoPage, Principal principal){
        final String userName = principal.getName();
        final User user = userService.loadUserByUserName(userName);
        return  answerResultsService.getPage(user.getId(),answerTopicVoPage);
    }

    @PostMapping
    public void save(@RequestBody MarkingVo markingVo ,Principal principal){
        final String username = principal.getName();
        final User user = userService.loadUserByUserName(username);
        answerResultsService.save(user.getId(),markingVo);
    }
}

